import { ADMIN_PROTOCOL } from '../actionTypes';
import fetch from '../utils/fetch';
import config from '../config';
import { message } from 'antd';

export const update = (payload) => ({
  type: ADMIN_PROTOCOL,
  payload,
});

export const updateContent = (content) => update(content);

// 获取协议信息
export const getProtocol = () => async (dispatch, getState) => {
  const res = await fetch({
    method: 'GET',
    url: `${config.host}/api/system/protocol`,
  }).catch(ex => console.error(ex));
  if (res.code === 200) {
    dispatch(update(res.data));
  } else {
    message.error('网络服务异常');
  }
};

/**
 * 编辑协议
 */
export const submit = () => async (dispatch, getState) => {
  const { protocol } = getState().admin;

  if (!protocol) {
    message.error('请填写协议');
    return false;
  }

  const res = await fetch({
    method: 'post',
    url: `${config.host}/api/system/protocol/edit`,
    param: {
      content: protocol,
    },
  }).catch(ex => console.error(ex));
  if (res.code === 200) {
    message.success('修改成功');
  } else {
    message.error('网络服务异常');
  }
};
